package com.freshcity.upload
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class BrowseComp extends Sprite
	{
		public function BrowseComp(arg1:AvatarModel)
		{
			super();
			this.init(arg1);
			this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onStage);
		}
		
		private function init(arg1:AvatarModel):void
		{
			var cutModel:AvatarModel;
			var labelFormat:TextFormat;
			
			var loc1:*;
			cutModel = arg1;
			labelFormat = new TextFormat("宋体,Arial,Helvetica", 12, 10066329);
			this.label = new TextField();
			with (this.label) 
			{
				selectable = false;
				autoSize = TextFieldAutoSize.LEFT;
				x = -3;
				y = 36;
				defaultTextFormat = labelFormat;
				text = "仅支持JPG、GIF、PNG图片文件，且文件小于2M";
			}
			addChild(this.label);
			this.btnBrowse = new SK_Browse();
			this.btnBrowsAddEvents();
			this._avatarModel = cutModel;
			this._fileRef = new FileReference();
			this._imgFilter = new FileFilter("Image Files (*.jpg, *.gif, *.jpeg, .*.png)", "*.jpg; *.gif; *.jpeg; *.png");
			addChild(this.btnBrowse);
			if (ExternalInterface.available) 
			{
				ExternalInterface.addCallback("setTicket", Param.setTicket);
			}
			Param.getTicket();
			return;
		}
		
		private function onStage(arg1:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onStage);
			this._parent = this.parent as CutView;
			return;
		}
		
		public function btnBrowsAddEvents():void
		{
			this.btnBrowse.addEventListener(MouseEvent.CLICK, this.selectLocalPic);
		}
		
		private function selectLocalPic(arg1:MouseEvent):void
		{
			if (this._parent.localPicArea.visible == false) 
			{
				this._parent.localPicArea.visible = true;
				this._parent.avatarArea.visible = true;
				this._parent.splitLines.visible = true;
				this._parent.cameraArea.visible = false;
				this.label.visible = true;
				if (this._parent.saveBtn != null) 
				{
					var loc1:*;
					this._parent.cancleBtn.visible = loc1 = true;
					this._parent.saveBtn.visible = loc1;
				}
				this._parent.cameraBtn.mouseEnabled = true;
				this._parent.cameraBtnAddEvents();
				this._parent.localPicArea.setLocalPicSize(this._avatarModel.bmd);
			}
			this._fileRef.browse([this._imgFilter]);
			this._fileRef.addEventListener(Event.SELECT, this.onFileSelected);
			this._fileRef.addEventListener(Event.CANCEL, this.onCancel);
			return;
		}
		
		private function onFileSelected(arg1:Event):void
		{
			this._fileRef.removeEventListener(Event.SELECT, this.onFileSelected);
			this._fileRef.removeEventListener(Event.CANCEL, this.onCancel);
			if (this.getFile()) 
			{
				this._fileRef.load();
				this._fileRef.addEventListener(Event.COMPLETE, this.refPicOK);
			}
			return;
		}
		
		private function onCancel(arg1:Event):void
		{
			this._fileRef.removeEventListener(Event.CANCEL, this.onCancel);
			return;
		}
		
		private function refPicOK(arg1:Event):void
		{
			this._avatarModel.imgData = this._fileRef.data;
			this._picLoader = new flash.display.Loader();
			this._picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.passImgToModel);
			this._picLoader.loadBytes(this._fileRef.data);
		}
		
		private function passImgToModel(arg1:Event):void
		{
			var loc1:*=arg1.target as flash.display.LoaderInfo;
			loc1.removeEventListener(Event.COMPLETE, this.passImgToModel);
			var loc2:*=loc1.content as flash.display.Bitmap;
			this._avatarModel.bmd = loc2.bitmapData;
			this._avatarModel.type = 2;
			loc1.loader.unload();
			return;
		}
		
		private function loadTempPic(arg1:String):void
		{
			var loc1:*=new flash.net.URLRequest(arg1);
			var loc2:*=new flash.display.Loader();
			var loc3:*=new LoaderContext(true);
			loc2.contentLoaderInfo.addEventListener(Event.COMPLETE, this.passImgToModel);
			loc2.load(loc1, loc3);
			return;
		}
		
		private function uploadFile():void
		{
			var _ticket:String;
			var urlrequest:flash.net.URLRequest;
			
			var loc1:*;
			_ticket = null;
			if (Param.ticket.length == 0) 
			{
				_ticket = "1";
			}
			else 
			{
				_ticket = Param.ticket[0][0];
			}
			urlrequest = new flash.net.URLRequest(Param.tmpUrl + "?ticket=" + _ticket + "&ct=" + new Date().getTime());
			Param.ticket.shift();
			Param.getTicket();
			try 
			{
				this._fileRef.upload(urlrequest);
			}
			catch (e:Error)
			{
			};
			this._fileRef.addEventListener(flash.events.DataEvent.UPLOAD_COMPLETE_DATA, this.startPhotoCut);
			this._fileRef.addEventListener(flash.events.IOErrorEvent.IO_ERROR, this.upTmpPhotoError);
			this._fileRef.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.upTmpPhotoError);
			return;
		}
		
		private function getFile():Boolean
		{
			var loc1:*;
			if (this._fileRef.size > 5242880) 
			{
				try 
				{
					ExternalInterface.call(Param.jsFunc, "M01108");
				}
				catch (e:Error)
				{
				};
				return false;
			}
			if (!new RegExp(".+\\.(jpg|png|gif|jpeg)$", "i").test(this._fileRef.name)) 
			{
				try 
				{
					ExternalInterface.call(Param.jsFunc, "M01107");
				}
				catch (e:Error)
				{
				};
				return false;
			}
			return true;
		}
		
		private function startPhotoCut(arg1:DataEvent):void
		{
			var evt:flash.events.DataEvent;
			var jsons:Object;
			
			var loc1:*;
			evt = arg1;
			this._fileRef.removeEventListener(flash.events.DataEvent.UPLOAD_COMPLETE_DATA, this.startPhotoCut);
			this._fileRef.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, this.upTmpPhotoError);
			this._fileRef.removeEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR, this.upTmpPhotoError);
			jsons = JSON.decode(evt["data"]);
			if (jsons["code"] != "A00006") 
			{
				try 
				{
					ExternalInterface.call(Param.jsFunc, jsons["code"]);
				}
				catch (e:Error)
				{
				};
				return;
			}
			else 
			{
				if (jsons["data"]["url"]) 
				{
					Param.tmpImgUrl = jsons["data"]["url"];
				}
				this.loadTempPic(Param.tmpImgUrl + "?ct=" + new Date().getTime());
			}
			return;
		}
		
		private function upTmpPhotoError(arg1:IOErrorEvent):void
		{
			this._fileRef.removeEventListener(IOErrorEvent.IO_ERROR, this.upTmpPhotoError);
			return;
		}
		
		public var label:TextField;
		
		public var btnBrowse:SimpleButton;
		
		private var _fileRef:FileReference;
		
		private var _imgFilter:FileFilter;
		
		private var _picLoader:flash.display.Loader;
		
		private var _avatarModel:AvatarModel;
		
		private var _fpVersion:int;
		
		private var _parent:CutView;
	}
}